package com.neusoft.elmboot.controller;


import com.neusoft.elmboot.po.Review;
import com.neusoft.elmboot.service.OrdersService;
import com.neusoft.elmboot.service.ReviewService;
import com.neusoft.elmboot.service.ex.DataNotFoundException;
import com.neusoft.elmboot.service.ex.DeleteException;
import com.neusoft.elmboot.service.ex.InsertException;
import com.neusoft.elmboot.service.ex.UpdateException;
import com.neusoft.elmboot.util.JsonResult;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reviews")
public class ReviewController {
    public static final int OK = 200;
    public static final int ERROR = 500;
    public static final int DATA_NOT_FOUND = 204;
    @Autowired
    private ReviewService reviewService;

    @PostMapping
    public JsonResult<Review> insertReview(@RequestBody Review review) {
        JsonResult<Review> result = new JsonResult<>();
        if (review == null) {
            result.setState(ERROR);
            result.setMessage("传过来的评论数据不能为空！");
            return result;
        }
        try {
            int data = reviewService.insertReview(review);
            result = new JsonResult<>(OK, review);
            result.setMessage("评论创建成功");
            return result;
        }catch (InsertException e) {
            result.setState(ERROR);
            result.setMessage(e.getMessage());
            return result;
        }
    }

    @DeleteMapping
    public JsonResult<Review> deleteReview(@RequestBody Review review) {
        JsonResult<Review> result = new JsonResult<>();
        if (review == null) {
            result.setState(ERROR);
            result.setMessage("传过来的评论数据不能为空！");
            return result;
        }
        try {
            int data = reviewService.deleteReview(review);
            result = new JsonResult<>(OK, review);
            result.setMessage("评论删除成功");
            return result;
        }catch (DeleteException e) {
            result.setState(DATA_NOT_FOUND);
            result.setMessage(e.getMessage());
            return result;
        }
    }

    @PutMapping
    public JsonResult<Review> updateReview(@RequestBody Review review) {
        JsonResult<Review> result = new JsonResult<>();
        if (review == null) {
            result.setState(ERROR);
            result.setMessage("传过来的评论数据不能为空！");
            return result;
        }
        try {
            int data = reviewService.updateReview(review);
            result = new JsonResult<>(OK, review);
            result.setMessage("评论更新成功");
            return result;
        }catch (UpdateException e) {
            result.setState(DATA_NOT_FOUND);
            result.setMessage(e.getMessage());
            return result;
        }
    }

    @GetMapping("/{businessId}")
    public JsonResult<List<Review>> selectReview(@PathVariable Integer businessId) {
        JsonResult<List<Review>> result = new JsonResult<>();
        if (businessId == null) {
            result.setState(ERROR);
            result.setMessage("传过来的商家不能为空！");
            return result;
        }
        try {
            List<Review> data = reviewService.selectReview(businessId);
            result = new JsonResult<>(OK, data);
            result.setMessage("评论查询成功!");
            return result;
        }catch (DataNotFoundException e) {
            result.setState(DATA_NOT_FOUND);
            result.setMessage(e.getMessage());
            return result;
        }

    }



}
